package fi.referencefatory.childphone;

import java.util.ArrayList;

import fi.referencefactory.childphone.R;

import android.app.Activity;
import android.app.ListActivity;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.provider.ContactsContract.CommonDataKinds.Phone;
import android.util.Log;
import android.view.HapticFeedbackConstants;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.CheckBox;

public class SelectContactsActivity extends Activity {
	private ArrayList<ContactItem> mContactsArray = new ArrayList<ContactItem>();
	private ArrayList<ContactItem> mSelectedContactsArray;
	private ContactsListAdapter mListAdapter;
	
	private ListView mContactListView = null;
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		Log.d("SelectContactsActivity", "onCreate");
		setContentView(R.layout.contacts_list);
		getContactsFromPb();
		mSelectedContactsArray = new ArrayList<ContactItem>();
		
		// Get view UI
		mContactListView = (ListView) findViewById(R.id.listView1);
		mListAdapter = new ContactsListAdapter(this, mContactsArray);
		mContactListView.setAdapter(mListAdapter);
		mContactListView.setFastScrollEnabled(true);
		mContactListView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View view, int position,
					long rowId) {
				Log.d("SelectContactsActivity", "onItemClick");
				view.performHapticFeedback(HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
				
				ContactItem item = mListAdapter.getItem(position);
				CheckBox chkBox = ( CheckBox ) view.findViewById(R.id.checkBox1);
				
				if (!mSelectedContactsArray.contains(item)) {
					Log.d("SelectContactsActivity", "onItemClick1");
					item.setSelected(true);
					chkBox.setChecked(true);
					mSelectedContactsArray.add(item);
				} else {
					item.setSelected(false);
					chkBox.setChecked(false);
					Log.d("SelectContactsActivity", "onItemClick2");
					mSelectedContactsArray.remove(item);
				}
					
				mListAdapter.notifyDataSetChanged();
				// TODO Auto-generated method stub
				
			}
		} );
	}
	
	
	private void getContactsFromPb() {
		mContactsArray.clear();
		
		String SELECTION = ContactsContract.Contacts.HAS_PHONE_NUMBER + "='1'";
		Cursor cursor = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI,
				new String[] {ContactsContract.Contacts._ID,
				ContactsContract.Contacts.DISPLAY_NAME,
				ContactsContract.Contacts.PHOTO_ID},
				SELECTION, null, "UPPER(" + ContactsContract.Contacts.DISPLAY_NAME + ") ASC");
		
		if (cursor != null) {
			Log.d("", "cursor != null");
			while (cursor.moveToNext()) {
				Log.d("SelectContactsActivity", "while");

		        int PhoneId = cursor.getInt(cursor.getColumnIndex(ContactsContract.Contacts._ID));
		        String name = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
		        Log.d("SelectContactsActivity", "name: "+ name);
		        String homePhone = "", cellPhone = "", workPhone = "", otherPhone = "";
		        Cursor contactPhones = getContentResolver().query(Phone.CONTENT_URI, null, Phone.CONTACT_ID + " = " + PhoneId, null, null);

		        while (contactPhones.moveToNext()) {

		            String number = contactPhones.getString(contactPhones.getColumnIndex(Phone.NUMBER));
		            int type = contactPhones.getInt(contactPhones.getColumnIndex(Phone.TYPE));
		            switch (type) {
		                case Phone.TYPE_HOME:   homePhone = number; break;
		                case Phone.TYPE_MOBILE:  cellPhone = number; break;
		                case Phone.TYPE_WORK:   workPhone = number; break;
		                case Phone.TYPE_OTHER:  otherPhone = number; break;
		                }
		        }        

		        ContactItem item = new ContactItem();
				item.setName(name);
				item.setNumber(((homePhone!="") ? homePhone : ((cellPhone!="") ? cellPhone : ((workPhone!="") ? workPhone : otherPhone))));
				mContactsArray.add(item);
		    }
		}
	}
	
	
	public void onDoneClicked(View v) {
		Log.d("SelectContactsActivity", "onDoneClicked");
		v.performHapticFeedback(HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
		
		// TODO: handling for selected contacts  -> save into DB?
		this.onBackPressed();
	}

}

